package com.it.shiro.controller;
import com.it.shiro.pojo.UserBean;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:LoginController
 * Package:com.it.shiro.controller
 * Description: 登录
 *
 * @date:2022/3/30 11:05
 * @author:动力节点
 */

@RestController
@RequestMapping("/common")
public class LoginController {

    private final Logger log = LoggerFactory.getLogger(LoginController.class);

    @RequestMapping("/login")
    public Object query(UserBean userBean) {

        Map<String, String> errorCode = new HashMap<>();
        //获取用户
        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated() && !currentUser.isRemembered()) {
            //将错误传输给前台
            UsernamePasswordToken token = new UsernamePasswordToken(userBean.getUserName(), userBean.getUserPass());
            token.setRememberMe(true);
            try {
                //shiro 请求登录逻辑的入口
                currentUser.login(token);
                //currentUser.getPrincipal() 来源自MyRealm对象的return simpleAuthenticationInfo对象的第一个属性
                currentUser.getSession().setAttribute("currentUser", currentUser.getPrincipal());
                return "login succeed";
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
                errorCode.put("errorMsg", "不存在的用户名");
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
                errorCode.put("errorMsg", "密码不正确");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
                errorCode.put("errorMsg", "账号被锁定");
            } catch (AuthenticationException authe) {
                log.info("Authentication error ", authe);
                errorCode.put("errorMsg", authe.getMessage());
            }
            return errorCode;
        }else {
            return "already login";
        }
    }
    /**
     * 获取当前用户
     */
    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(){
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        return session.getAttribute("currentUser");
    }
    /**
     * 登出
     */
//    @RequestMapping("/logout")
//    public void logout(){
//        Subject currentUser = SecurityUtils.getSubject();
//        currentUser.logout();
//    }

    /**
     * 为授权权限
     */
    @RequestMapping("/unauthorized")
    public String unauthorized(){
        return "未经授权，禁止访问";
    }
}
